package it.sebsibenal.server;

import java.net.URL;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.docs.FolderEntry;
import com.google.gdata.util.AuthenticationException;

public class GoogleDocsService {

	private DocsService getService() {
		try {
			DocsService service = new DocsService("folk-project");
			service.setUserCredentials("sebsibenal@gmail.com", "s3bs1b3nal");
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public void createFolder(String title) {
		try {
			DocsService service = getService();
			DocumentListEntry newEntry = new FolderEntry();
			newEntry.setTitle(new PlainTextConstruct(title));
			URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/");
			service.insert(feedUrl, newEntry);
		} catch (Exception e) {
			throw new RuntimeException("Errore durante la creazione della cartella");
		}
	}
	
	public String getFolders() {
		String res = "";
		try {
			DocsService service = getService();
			URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/");
			DocumentListFeed feed = service.getFeed(feedUrl, DocumentListFeed.class);
			for (DocumentListEntry entry : feed.getEntries()) {
				res += entry.getTitle().getPlainText() + " - ";
			}
		} catch (Exception e) {
			res = "Errore durante la lettura delle cartelle";
		}
		return res;
	}
	
}
